package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.OrderBean;
import com.project.bean.OrderDetailBean;
import com.project.mapper.IOrderMapper;
import com.project.service.IOrderService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * 订单业务实现类
 */
public class OrderServiceImpl extends BaseService implements IOrderService {
    @Override
    public CutPageBean<OrderBean> cutByItem(Integer pageNO, String receiver) {
        CutPageBean<OrderBean> cutPageBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        List<OrderBean> list = mapper.cutByItemList((pageNO*CutPageBean.PAGESIZE),CutPageBean.PAGESIZE,receiver);
        cutPageBean.setList(list);
        cutPageBean.setCount(mapper.getTotalByItem(receiver));
        session.close();
        return cutPageBean;
    }

    @Override
    public void add(OrderBean order,OrderDetailBean orderDetail) {
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        mapper.add(order);

        orderDetail.setOrderId(order.getOrderId());

        mapper.addDetail(orderDetail);
        session.commit();
        session.close();
    }

    @Override
    public OrderBean findById(Integer orderId) {
        SqlSession session = this.getSession();
        IOrderMapper mapper = session.getMapper(IOrderMapper.class);
        OrderBean order = mapper.findById(orderId);
        session.close();
        return order;
    }
}
